import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { PublicService } from '../public.service';
import { SELECT_OPTIONS } from 'app/app.constants';
import { Subject, Observable } from 'rxjs';
import { NotificationService } from 'app/shared/service';

@Component({
    selector: 'itmp-vms-set',
    templateUrl: './vms-set.component.html',
    styleUrls: ['./vms-set.component.css']
})
export class VmsSetComponent implements OnInit {
    @Input() data: any;
    @Output() initdata = new EventEmitter<any>();
    illumModeList: Array<any>;
    allMessageData = []; // vms消息库
    details: any;
    currentPage = 0;
    previewPage = 0;
    currentPageImgs = []; // 目前vms图片
    previewPageImgs = [];
    isOkLoading = false;
    selectData: any;
    now = new Date().getTime();
    constructor(private pub: PublicService, private msg: NotificationService) {}
    ngOnInit() {
        this.getAllMessageData();
        this.getSelectionList();
        this.getVmsCurrentStatusById();
    }
    getAllMessageData() {
        const allMessageData = [];
        this.pub.getAllVmsLibraryMessage().subscribe(res => {
            res.data.forEach(item => {
                const texts = [];
                item.texts.forEach(text => {
                    texts.push(text.text);
                });
                allMessageData.push({
                    message: item.url ? item.url.split(',')[0] : '',
                    content: texts.join(','),
                    ...item
                });
            });
            this.allMessageData = allMessageData;
        });
    }
    // getPageData() {
    //     this.pub.getVmsCurrentMessageById(this.data.id).subscribe(res => {
    //         const temp = res.data;
    //         this.currentPageImgs = temp.template ? temp.template.url.split(',') : [];
    //     });
    // }
    getSelectionList() {
        setTimeout(() => {
            this.illumModeList = this.pub.getSelectOptionsList(SELECT_OPTIONS.ILLUM_MODE).data || [];
        });
    }
    getVmsCurrentStatusById() {
        this.pub.getVmsCurrentStatusById(this.data.id || this.data.eqtId).subscribe(res => {
            this.details = res.data;
            this.currentPageImgs = res.data && res.data.currentMessage.url ? res.data.currentMessage.url.split(',') : [];
            this.initdata.emit(this.details);
        });
    }
    clickTR(value) {
        this.previewPage = 0;
        this.selectData = value;
        this.previewPageImgs = value.url.split(',');
    }
    goToPage(type, flag) {
        type === 'current' ? (this.currentPage = flag) : (this.previewPage = flag);
    }
    // 保存修改
    handleOk(parentData): Observable<any> {
        const _ss = new Subject();
        // const _illumss = new Subject();
        // const _vmsMessagess = new Subject();
        const illumParmas = {
            eqtId: this.data.id || this.data.eqtId,
            illumLevel: parentData.illumLevel,
            illumMode: parentData.illumMode
        };
        if (!illumParmas.illumLevel || !illumParmas.illumMode) {
            this.msg.notify({ type: 'warning', path: 'EquipmentManagement.vmsModal.save_Illum_failed' });
            setTimeout(() => {
                _ss.next('fail');
            });
        } else {
            this.pub.setVmsIllumBrightness(illumParmas).subscribe(res => {
                if (res.messagecode === '000000') {
                    _ss.next('success');
                    this.msg.notify({ type: 'success', path: 'EquipmentManagement.vmsModal.save_Illum_success' });
                } else {
                    _ss.next('fail');
                    this.msg.notify({ type: 'warning', path: 'EquipmentManagement.vmsModal.save_Illum_failed' });
                }
            });
        }
        const vmsGraphicMsgPageInfos = [];
        if (this.selectData) {
            this.selectData.url.split(',').forEach((item, index) => {
                vmsGraphicMsgPageInfos.push({
                    pageSeq: index + 1,
                    vmsGraphicMessages: [
                        {
                            coor_x: 0,
                            coor_y: 0,
                            url: item
                        }
                    ]
                });
            });
            const vmsParams = {
                deviceId: this.data.id || this.data.eqtId,
                ipAddress: this.data.ip || this.data.ipAddr,
                messageId: this.selectData.msgId,
                vmsGraphicMsgPageInfos
            };
            this.pub.sendVmsMessage(vmsParams).subscribe(res => {
                if (res.messagecode === '000000') {
                    this.msg.notify({ type: 'success', path: 'EquipmentManagement.vmsModal.save_VMS_success' });
                    // _vmsMessagess.next('success');
                } else {
                    this.msg.notify({ type: 'warning', path: 'EquipmentManagement.vmsModal.save_VMS_failed' });
                    // _vmsMessagess.next(null);
                }
            });
        }
        return _ss.asObservable();
    }
}
